package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.*;
import com.itheima.service.db.*;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SettingManager {

    @DubboReference
    QuestionService questionService;

    @DubboReference
    NotificationService notificationService;

    @DubboReference
    private BlackListService blackListService;

    @DubboReference
    private UserInfoService userInfoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @DubboReference
    private UserService userService;

    //查询用户通知设置
    public ResponseEntity findSetting() {
        //1.获取当前用户
        User user = UserHolder.get();

        //2.远程调用service查询问题
        Question question = questionService.findByUserId(user.getId());
        //3.远程调用service查询通知设置
        Notification notification = notificationService.findByUserId(user.getId());

        //4.封装vo对象且返回
        SettingVo vo = new SettingVo();

        vo.setId(user.getId());
        vo.setPhone(user.getMobile());

        //设置陌生人问题
        if (question != null) {
            vo.setStrangerQuestion(question.getStrangerQuestion());
        }

        //设置 通知设置
        if (notification != null) {
            vo.setPinglunNotification(notification.getPinglunNotification());
            vo.setLikeNotification(notification.getLikeNotification());
            vo.setGonggaoNotification(notification.getGonggaoNotification());
        }

        return ResponseEntity.ok(vo);
    }

    // 设置陌生人问题
    public void setQuestion(String content) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.根据userId查陌生人问题
        Question question = questionService.findByUserId(userId);
        // 3.判断处理
        if (question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            // 新增
            questionService.save(question);
        } else {
            question.setStrangerQuestion(content);
            // 修改
            questionService.update(question);

        }
    }

    // 设置推送通知
    public void setNotification(Notification param) {
        //1.获取当前用户id
        Long userId = UserHolder.get().getId();

        //2.根据用户id查询用户的通知设置
        Notification notification = notificationService.findByUserId(userId);

        //3.判断通知对象是否为空
        if (notification == null) {
            //3.1 若为空,则保存
            param.setUserId(userId);
            notificationService.save(param);
        } else {
            //3.2 否则则修改
            param.setId(notification.getId());
            notificationService.update(param);
        }
    }

    public ResponseEntity findBlackListByPage(Integer pageNum, Integer pageSize) {
        //1.获取当前用户id
        Long userId = UserHolder.get().getId();

        //2.调用service分页查询,返回pageBeanVo(集合中的黑名单只用用户id)
        PageBeanVo vo = blackListService.findByPage(pageNum, pageSize, userId);

        //3.获取分页对象中的集合,获取每个黑名单对象,调用userInfoService查询黑名单用户信息,将userInfo放入一个新集合中
        //3.0 创建一个集合 用来存放黑名单用户信息
        List<UserInfo> userInfos = new ArrayList<>();

        //3.1 获取分页对象中的集合,遍历获取每个黑名单对象
        List<BlackList> items = (List<BlackList>) vo.getItems();
        if (CollUtil.isNotEmpty(items)) {
            items.forEach(e -> {
                //3.2 调用userInfoService查询黑名单用户信息
                Long id = e.getBlackUserId();
                UserInfo userInfo = userInfoService.findByUserId(id);

                //3.3 将userInfo放入一个新集合中
                userInfos.add(userInfo);
            });
        }

        //4.将新集合设置给pageBeanVo对象,且返回
        vo.setItems(userInfos);
        return ResponseEntity.ok(vo);
    }

    // 移除黑名单
    public void deleteBlackList(Long blackId) {
        //1.获取当前用户id
        //2.调用service删除黑名单
        blackListService.delete(UserHolder.get().getId(), blackId);
    }

    //校验验证码
    public ResponseEntity checkVerificationCode(String verificationCode, String phone) {

        //1.判断验证码是否和redis中的一致,若不一致,返回500
        String redisCode = stringRedisTemplate.opsForValue().get(ConstantUtil.SMS_CODE + phone);
        if (!StrUtil.equals(verificationCode, redisCode)) {
            HashMap<String,Boolean> map=new HashMap<>();
            map.put("verification",false);
            return ResponseEntity.ok(map);
        }
        //5.1 从redis中把验证码删除
        stringRedisTemplate.delete(ConstantUtil.SMS_CODE + phone);
        HashMap<String,Boolean> map=new HashMap<>();
        map.put("verification",true);
        return ResponseEntity.ok(map);
    }


}
